package com.goods.service.impl;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.service.ConsumerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    @Override
    public List<Consumer> findConsumerList(String name, String address, String contact) {
        List<Consumer> consumers = null;
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(address) && StringUtils.isEmpty(contact)){
            consumers = consumerMapper.selectAll();
        }else {
            consumers = consumerMapper.selectByNameAddressContact(name,address,contact);
        }

        return consumers;
    }

    @Override
    public int add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        return consumerMapper.insert(consumer);
    }

    @Override
    public Consumer edit(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        return consumerMapper.selectOne(consumer);
    }

    @Override
    public int update(Long id,ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        Consumer consumer1 = consumerMapper.selectOne(consumer);
        BeanUtils.copyProperties(consumerVO,consumer1);
        consumer1.setModifiedTime(new Date());
        return consumerMapper.updateByPrimaryKey(consumer1);
    }

    @Override
    public int delete(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        return consumerMapper.delete(consumer);
    }

    @Override
    public List<Consumer> findAll() {
        return consumerMapper.selectAll();
    }
}
